package com.styytech.dadabao.test.clippicture;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.styytech.dadabao.R;
import com.styytech.dadabao.widge.clippicture.ClipPictureBaseActivity;
import com.styytech.dadabao.widge.clippicture.ClipView;

/**
 * @图片的放缩
 * @移动、截图
 */
@SuppressLint("ResourceAsColor")
public class ClipPictureActivity extends ClipPictureBaseActivity implements
        OnClickListener {

    private ClipView clipview;
    private ImageView srcPic;
    private ImageView iv_rotateBtn;
    Context context;
    private Button btn_ok;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clip_picture);
        context = this;
        findView();

        srcPic.setOnTouchListener(this);
        btn_ok.setOnClickListener(this);
        iv_rotateBtn.setOnClickListener(this);

        // 将原图片按照手机屏幕的宽高进行缩放
        zoomPicture(srcPic);
    }

    private void findView() {
        srcPic = (ImageView) this.findViewById(R.id.src_pic);
        btn_ok = (Button) this.findViewById(R.id.btn_ok);
        iv_rotateBtn = (ImageView) this.findViewById(R.id.iv_rotateBtn);
        clipview = (ClipView) this.findViewById(R.id.clipview);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ok:
                // 1.获取矩形区域内的截图
                Bitmap fianBitmap = getBitmap(clipview);
                // 2.保存矩形区域内的截图到CAPTUREPATH目录中，其他地方读取这个目录里面的图片即可
                saveBitmap(fianBitmap, 100);
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.iv_rotateBtn:
                if (NewBitmap != null) {
                    // 旋转图片，由于有些机子拍照后是横屏显示的，所以这里大多数只是为了矫正图片
                    NewBitmap = rotaingImageView(90, NewBitmap);
                    srcPic.setImageBitmap(NewBitmap);
                }
                break;

            default:
                break;
        }
    }

}